﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Linq.Expressions;
using System.Collections;

namespace zUfDsl.Common
{
    class handleSQL
    {
        private static SqlConnection cn = null;
        public static SqlConnection getConnect()
        {
            if (handleSQL.cn == null || handleSQL.cn.State == ConnectionState.Closed)
            {
                handleSQL.cn = new SqlConnection();
                handleSQL.cn.ConnectionString = cHandleUF.cConnstr;
                handleSQL.cn.Open();
            }
            return handleSQL.cn;

        }


        public static DataTable getdata(string cmdsql,params SqlParameter[] paras)
        {
            var cn = Common.handleSQL.getConnect();
            var cmd = cn.CreateCommand();
            return cmd.GetData(cmdsql, paras);
        }


        public static List<T> GetData<T>(Expression<Func<T, bool>> whereExp)
        {
            var cn = Common.handleSQL.getConnect();
            var cmd = cn.CreateCommand();
            return cmd.Query<T>(whereExp);
        }

        public static T GetDataFirst<T>(Expression<Func<T, bool>> whereExp)
        {
            var cn = Common.handleSQL.getConnect();
            var cmd = cn.CreateCommand();
            return cmd.First<T>(whereExp);
        }

        public static object GetObj(string cmdsql,params SqlParameter[] paras)
        {
            
            var cn = Common.handleSQL.getConnect();
            var cmd = cn.CreateCommand();
            return cmd.GetObj(cmdsql, paras);             
        }

        public static int execSQL(string cmdsql, params SqlParameter[] paras)
        {
            var cn = Common.handleSQL.getConnect();
            var cmd = cn.CreateCommand();
            return cmd.ExecSQL(cmdsql, paras);
        }

    }
}


